home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / sound / players / mp02_tar.z / mp02_tar / mp02 / userio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-25  |  4.8 KB  |  154 lines

  1. /* userio.c -- handy user interface functions */
  2.  
  3. /*****************************************************************************
  4. *        Change Log
  5. *  Date        | Change
  6. *-----------+-----------------------------------------------------------------
  7. * 21-May-86 | Created
  8. *****************************************************************************/
  9.  
  10. #include "cext.h"
  11. #include "stdio.h"
  12. #include "userio.h"
  13.  
  14.  
  15. /****************************************************************************
  16. *                askbool
  17. * Inputs:
  18. *    char *prompt: string to prompt for user input
  19. *    int deflt: true or false default
  20. * Returns:
  21. *    boolean: true or false as entered by user
  22. * Effect:
  23. *    prompts user for yes or no input, returns result
  24. ****************************************************************************/
  25.  
  26. int askbool(prompt, deflt)
  27.     char *prompt;
  28.     int deflt;
  29. {
  30. #define undefined -1
  31.     char defchar;    /* the default answer */
  32.     char c;        /* user input */
  33.     int result = -1;    /* the result: -1 = undefined, 0 = false, 1 = true */
  34.     if (deflt) defchar = 'y';
  35.     else defchar = 'n';
  36.     while (result == undefined) {
  37.     fprintf(stderr, "%s? [%c]: ", prompt, defchar);
  38.     c = getchar();
  39.     if (toupper(c) == 'Y') result = true;
  40.     else if (toupper(c) == 'N') result = false;
  41.     else if (c == '\n') result = deflt;
  42.     else fprintf(stderr, "Please type Y or N.\n");
  43.     }
  44.     while (c != '\n') c = getchar();    /* flush the input line */
  45.     return result;
  46. }
  47.  
  48.  
  49. /****************************************************************************
  50. *                fileopen
  51. * Inputs:
  52. *    char *deflt: the default file name (e.g. from command line)
  53. *    char *extension: default extension
  54. *    char *mode: read ("r") or write ("w")
  55. *    char *prompt: prompt for user
  56. * Returns:
  57. *    opened file pointer
  58. * Effect: 
  59. *    opens file, prompts for user input if necessary and warns about
  60. *    possible confusion.  If deflt is a null string, the user will
  61. *    be prompted for a name.     The routine loops until a file is opened.
  62. *    If the mode is "r", a check is made to see if the file exists
  63. *    with and without the extension.     If both exist a warning is given.
  64. *    For mode "w", a check is made to see if the file will be overwritten.
  65. *    The extension is automatically added if the default or user-typed
  66. *    file has no "."     At the bottom of the loop body, if no file has
  67. *    been opened, the user is prompted for another file name.
  68. ****************************************************************************/
  69.  
  70. FILE *fileopen(deflt, extension, mode, prompt)
  71.     char *deflt;
  72.     char *extension;    /* default extension */
  73.     char *mode;        /* read "r" or write "w" */
  74.     char *prompt;    /* prompt for user */
  75. {
  76.     char filename[100]; /* trial name */
  77.     char extname[100];    /* trial name with extension added */
  78.     FILE *fp = NULL;    /* file corresponding to filename */
  79.     FILE *fpext;    /* file corresponding to extname */
  80.     char *problem;    /* tells user why he has to try again */
  81.  
  82.     strcpy(filename, deflt);
  83.     while (fp == NULL) {    /* keep trying until a good file is found */
  84.     while (strlen(filename) == 0) { /* avoid null file names */
  85.         fprintf(stderr, "%s: ", prompt);
  86.         fgets(filename, 99, stdin);
  87.         if (strlen(filename) > 0)
  88.         filename[strlen(filename)-1] = '\0';
  89.     }
  90.     if (mode[0] == 'r') {
  91.         strcpy(extname, filename);
  92.         strcat(extname, ".");
  93.         strcat(extname, extension);
  94.         fp = fopen(filename, mode);
  95.         if (fp != NULL) fpext = NULL; /* added 1/30/93 --gl */
  96.         else fpext = fopen(extname, mode);
  97.         if (fp != NULL && fpext != NULL) {
  98.         fprintf(stderr,
  99.             "warning: both %s and %s exist.     %s will be used.\n",
  100.             filename, extname, filename);
  101.         fclose(fpext);
  102.         } else if (fpext != NULL) {
  103.         fp = fpext;
  104.         }
  105.         if (fp == NULL) problem = "Couldn't find %s.";
  106.     } else if (mode[0] == 'w') {
  107.         /* add the extension if there is no '.' in the file name */
  108.         if (!strchr(filename, '.')) {
  109.         strcat(filename, ".");
  110.         strcat(filename, extension);
  111.         }
  112.         fp = fopen(filename, "r");
  113.         if (fp != NULL) {
  114.         char question[100];
  115.         fclose(fp);
  116.         strcpy(question, "OK to overwrite ");
  117.         strcat(question, filename);
  118.         if (askbool(question, false)) {
  119.             fp = fopen(filename, mode);
  120.         } else {
  121.             fp = NULL;
  122.             problem = "";
  123.         }
  124.         } else {
  125.         fp = fopen(filename, mode);
  126.         if (fp == NULL) problem = "Couldn't create %s.";
  127.         }
  128.     }
  129.     if (fp == NULL) {
  130.         fprintf(stderr, problem, filename);
  131.         fprintf(stderr, "  Try again.\n%s: ", prompt);
  132.         fgets(filename, 99, stdin);
  133.         if (strlen(filename) > 0)
  134.         filename[strlen(filename)-1] = '\0';
  135.     }
  136.     }
  137.     return fp;
  138. }
  139.  
  140. /****************************************************************************
  141. *                    readln
  142. * Inputs:
  143. *    FILE * fp: File to read from
  144. * Effect: 
  145. *    Reads and discards characters until a newline is seen
  146. ****************************************************************************/
  147.  
  148. void readln(fp)
  149. FILE *fp;
  150. {
  151.     while (getc(fp) != '\n')
  152.         ;
  153. }
  154.